//
//  LCWEmotionKeyboard.swift
//  MyWB
//
//  Created by 李成万 on 16/7/4.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit

class LCWEmotionKeyboard: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupUI(){
        backgroundColor = randomColor()
        addSubview(emotionToolBar)
        
        emotionToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        
        emotionToolBar.emotionToolBarBTnClickClosure = {[weak self] (type:emotionToolBarType)->() in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            self?.pageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            //此处和最下方,这句话出现了两次,是为了在点击按钮切换表情和滑动切换表情时候,总页数都能显示正确
            self?.pageControl.numberOfPages = LCWEmotionTools.sharedTools.emotionss[indexPath.section].count
            self?.pageControl.currentPage = 0
            
        }
   
        addSubview(pageView)
        pageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(emotionToolBar.snp_top)
        }
        
        addSubview(pageControl)
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(pageView)
            make.bottom.equalTo(pageView)
        }
        
        
    }
    
    
    
    private lazy var emotionToolBar : LCWEmotionToolBar = LCWEmotionToolBar()
    
    private lazy var pageView : LCWEmotionPageView = {
        let view = LCWEmotionPageView()
        view.bounces = false
        view.pagingEnabled = true
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        view.delegate = self
        return view
    }()
    
    
    private lazy var pageControl : UIPageControl = {
        let pageControl = UIPageControl()
        
        pageControl.numberOfPages = LCWEmotionTools.sharedTools.emotionss[0].count
        pageControl.currentPage = 0
        
//        pageControl.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
//        pageControl.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        return pageControl
    }()
}

// MARK: - 代理方法写在这里是因为 pageView 移动之后需要告知 toolbar 更改按钮,而 pageView 和 toolBar 没有直接关联,所以需要写在他们的共同父类中
extension LCWEmotionKeyboard : UICollectionViewDelegate{
    /*
    - 计算方式 （只试用于我么cell和pageView 一样大）
    - 当前的pageView 界面上展示给用户最多两个cell
    - 相对于 pageView 左侧起点为O点 屏幕中心点x = 屏幕的宽度/ 2 + contentOffset.x
    - 由于cell贴在pageView 每个cell都有自己的frame
    - 如果我判断出 哪个cell的frame 包含我们屏幕的中心点 就代表他显示的多
    - 通过cell 和 pageView 得到indexPath
    - indexPath 的到section
    - section 0 1 2  viewWithTag  通过该方法 得到button  吧button状态发生改变
    */
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
//        print(scrollView.contentOffset.x)
        
        let centerPointX = pageView.center.x + scrollView.contentOffset.x
//        print(centerPointX)
        
        let cellArr = pageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        
        var indexPath : NSIndexPath

        if cellArr.count == 2{
            let cellf = cellArr.first!
            let celll = cellArr.last!
            if cellf.frame.contains(CGPoint(x: centerPointX, y: 0)){
                indexPath = pageView.indexPathForCell(cellf)!
            }else{
                indexPath = pageView.indexPathForCell(celll)!
            }
            
            emotionToolBar.setCurrentButton(indexPath.section)
            self.pageControl.numberOfPages = LCWEmotionTools.sharedTools.emotionss[indexPath.section].count
            
            self.pageControl.currentPage = indexPath.item
        }
        
    }
}